import json
import os
from pymongo import MongoClient

json_path1 = 'D:/PycharmProjects/pingan_data/json_data/'
json_path2 = 'D:/PycharmProjects/xywy_data/json_data/'
json_path3 = 'D:/PycharmProjects/39html_data/json_data/'

db_conf = {
    'host': '192.168.1.3',
    'port': 31935
}
conn = MongoClient("mongodb://xiaotuo:n8nRn78BirC8xnvM@192.168.1.3:31935/pingan")

db = conn.pingan
collection = db['39']


# print(db.collection_names())  # ['disease']
#
# for i in range(1, 6793):
#     print(i)
#     try:
#         with open(json_path + str(i) + '.json', mode='r', encoding='utf-8') as f:
#             json_data = json.load(f)
#     except:
#         continue
#     collection.insert(json_data)


def insert_db(path):
    i = 0
    for filename in os.listdir(path):
        print(i + 1)
        try:
            with open(os.path.join(path, filename), mode='r', encoding='utf-8') as f:
                json_data = json.load(f)
        except:
            continue
        collection.insert(json_data)
        i = i + 1


if __name__ == '__main__':
    insert_db(json_path2)
